//
//  SCTabBar.swift
//  weibo
//
//  Created by 阿城 on 15/12/5.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit

class SCTabBar: UITabBar {

    
    var composeClick: (()->())?
    
    lazy var button: UIButton? = {

        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.sizeToFit()
        
        self.addSubview(btn)
        btn.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    @objc private func composeButtonClick(){
        composeClick?()
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundImage = UIImage(named:"tabbar_background")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    class func getTabBar(frame: CGRect)->SCTabBar {
//    
//        return SCTabBar(frame: frame)
//    
//    }
    
    override func layoutSubviews() {
        super.layoutSubviews()///!!!!!!!!!!!!!!!!!!!!!!!!
        
        //添加composeButton
        button?.center = CGPoint(x: frame.width * 0.5, y: frame.height * 0.5)
        
        
        
        let btnW = frame.width / 5
//        printLog("--------")
        
        var idx = 0
        
        for sub in subviews{

            if sub.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                let num = idx < 2 ? idx : idx + 1
                sub.frame.size.width = btnW
                sub.frame.origin.x = btnW * CGFloat(num)
//                printLog(sub.frame)
                idx++
            }
        }
    }
    
    
    

}
